//
//  Discover_MindCell.m
//  MindBox
//
//  Created by MS on 16-1-6.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "Discover_MindCell.h"
#import "ImageTitleView.h"
#import "UIImageView+WebCache.h"
#import "UIView+Frame.h"
#import "ScrollCellView.h"
#import "Discover_MindItem.h"
@interface Discover_MindCell ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>

@property (nonatomic,weak) ImageTitleView * imagV;
@property (weak, nonatomic) IBOutlet UIView *content_view1;
@property (weak, nonatomic) IBOutlet UIView *content_view2;

@property (nonatomic,weak) UIScrollView * scrollView;
@property (nonatomic,strong) NSMutableArray * scrollCellViewArr;

@property (nonatomic,strong) NSArray * itemsArray;
@property (nonatomic,weak) UICollectionView * collectionView;

@end


@implementation Discover_MindCell

-(void)setActivity:(Activity *)activity
{
    _activity = activity;
    [self.imagV.imageView sd_setImageWithURL:[NSURL URLWithString:activity.url]];
    self.imagV.titleView.text = activity.title;
    self.imagV.subTitleView.text = activity.my_description;

    self.itemsArray = activity.goods;
    self.collectionView.contentOffset = CGPointZero; //contentInset = UIEdgeInsetsZero;
    [self.collectionView reloadData];
}


- (void)awakeFromNib {
    // Initialization code
    self.contentView.backgroundColor = [UIColor colorWithRed:0.94f green:0.94f blue:0.94f alpha:1.00f];
    self.itemsArray = [[NSArray alloc] init];
    self.scrollCellViewArr = [[NSMutableArray alloc] init];
    
    ImageTitleView * view0 = [ImageTitleView imageTitleViewWithXib];
    view0.frame = self.content_view1.bounds;
    self.imagV = view0;
    _imagV.tag = -1;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    [_imagV addGestureRecognizer:tap];
    [self.content_view1 addSubview:view0];

    [self creatCollectionView];
}


-(void)creatCollectionView
{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 10;
    
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.autoresizesSubviews = YES;
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.content_view2 addSubview:collectionView];
    collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView = collectionView;
    collectionView.delegate = self;
    collectionView.dataSource  = self;
    collectionView.bounces = NO;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemsArray.count+1;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Discover_MindItem * item = [Discover_MindItem cellWithcollectionView:collectionView cellForItemAtIndexPath:indexPath];
    
    if (indexPath.item<_itemsArray.count) {
          item.goods = self.itemsArray[indexPath.item];
    }
    else{
        item.imageView.image =[UIImage imageNamed:@"looks"];
        item.titleView.text = nil;
        item.priceView.text = nil;
    }
    return item;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((self.width- 35)/3.5, self.collectionView.height-10);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 10, 5, 10);
}


-(void)tapClick:(UIGestureRecognizer *)tap
{
    NSInteger index = tap.view.tag;
 
    if ([self.delegate respondsToSelector:@selector(discover_mindCellDidClick:activity:goods:index:)]) {
        [self.delegate discover_mindCellDidClick:self activity:self.activity goods:nil index:index];
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSInteger index = indexPath.item;
    Goods * goods = nil;
    if (indexPath.item<self.itemsArray.count) {
        goods = self.activity.goods[indexPath.item];
    }
    else{
        index = -1;
    }
    
    if ([self.delegate respondsToSelector:@selector(discover_mindCellDidClick:activity:goods:index:)]) {
        [self.delegate discover_mindCellDidClick:self activity:self.activity goods:goods index:index];
    }
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    /**
     *  获取自动布局之后的真实frame需要加以下两种方法
     */
    [self.contentView setNeedsLayout];
    [self.contentView layoutIfNeeded];
    
    self.imagV.frame = self.content_view1.bounds;
    self.collectionView.frame = self.content_view2.bounds;

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    // Configure the view for the selected state
}
@end
